package com.logic.landseaserver.models.payment.channel;

import java.util.Date;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.logic.common.security.SecurityUtils;
import com.logic.landseaserver.common.exception.LandseaException;
import com.logic.landseaserver.common.util.LandSeaUtil;
import com.logic.landseaserver.domain.BillPayment;
import com.logic.landseaserver.models.payment.PaymentConstant;
import com.logic.landseaserver.models.payment.PaymentConstant.PayBank;
import com.logic.landseaserver.models.payment.PaymentConstant.BillPaymentStatus;
import com.logic.landseaserver.models.payment.bean.PaymentHelperBean;
import com.logic.landseaserver.models.payment.bean.PaymentQueryRequest;
import com.logic.landseaserver.models.payment.bean.PaymentQueryResult;
import com.logic.landseaserver.models.payment.bean.PaymentRequest;
import com.logic.landseaserver.models.payment.bean.ShiJiPosPaymentResponse;
import com.logic.landseaserver.persistence.write.BillPaymentWriteMapper;

/**
 * 
 *
 * 项目名称：system-server</br>
 * 类名称：ShiJiPosBillPaymentGenerator</br>
 * 类描述：银石POS支付</br>
 * 创建人：Aaron</br>
 * 创建时间：2017年7月17日 下午11:11:19</br>
 * @version 1.0
 *
 */
@Service(PaymentConstant.GENETRATE_BILLPAYMENT_PREFIX + PaymentConstant.PayChannel.SHIJI_POS)
public class ShiJiPosPaymentChannel extends AbstractPaymentChannel
{
    private static final Logger LOGGER = LoggerFactory.getLogger(ShiJiPosPaymentChannel.class);
    
    /** 支付成功 **/
    private static final String RESULT_SUCCESS = "0";
    
    /** 操作成功 **/
    public static final int OPERATE_SUCCESS = 0;
    
    @Autowired
    private BillPaymentWriteMapper billPaymentWriteMapper;
    
    @Override
    public BillPayment buildBillPayment(PaymentRequest paymentRequest)
        throws LandseaException
    {
        // 构造BillPayment
        BillPayment insertBillPayment = new BillPayment();
        insertBillPayment.setMoney(LandSeaUtil.get2Double(paymentRequest.getMoney()));
        insertBillPayment.setBank(PayBank.SHIJI_POS);
        
        return insertBillPayment;
    }

    @Override
    public void sendPayRequest(BillPayment billPayment, PaymentHelperBean helperObject, Map<String, Object> resultMap)
        throws LandseaException
    {
        // 不需要发送到第三方，直接页面上生成二维码，扫码后请求查询账单接口
    }

    /**
     * 
     * [描述]：处理响应结果</br>
     *
     * @param billPayment
     * @param paymentResponse
     * @param resultMap
     * @throws LandseaException
     *
     */
    @Override
    public boolean analysisResult(BillPayment billPayment, Object response, PaymentHelperBean helperObject, Map<String, Object> resultMap)
        throws LandseaException
    {
        String logInfo = getLog(billPayment);
        
        // 账单已支付，直接返回成功
        if (BillPaymentStatus.SUCCESS.equals(billPayment.getStatus()))
        {
            LOGGER.info(logInfo + "账单已支付成功");
            resultMap.put("status", OPERATE_SUCCESS);
            resultMap.put("msg", "success");
            return PaymentConstant.RESPONSE_PROCESS_STOP;
        }
        
        ShiJiPosPaymentResponse paymentResponse = (ShiJiPosPaymentResponse)response;
        String payResult = paymentResponse.getPayResult();
        
        if (RESULT_SUCCESS.equals(payResult))
        {
            billPayment.setTransactionNo(paymentResponse.getTransactionNo());
            billPayment.setStatus(BillPaymentStatus.SUCCESS);
        }
        else
        {
            // 支付失败
            billPayment.setStatus(BillPaymentStatus.FAILED);
            // 失败的时候记录描述
            billPayment.setComments(paymentResponse.getMsg());
            
            // 更新支付记录表，支付失败，流程结束
            BillPayment paymentUpdate = new BillPayment();
            paymentUpdate.setId(billPayment.getId());
            paymentUpdate.setStatus(billPayment.getStatus());
            paymentUpdate.setTime(new Date());
            paymentUpdate.setComments(billPayment.getComments());
            // 添加公共字段
            paymentUpdate.updateCommonInfo(SecurityUtils.getCurrentLogin().getUserId());
            billPaymentWriteMapper.updateByPrimaryKeySelective(paymentUpdate);
            
            // 返回结果
            LOGGER.info(logInfo + "第三方支付失败");
            resultMap.put("status", OPERATE_SUCCESS);
            resultMap.put("msg", "success");
            return PaymentConstant.RESPONSE_PROCESS_STOP;
        }
        
        return PaymentConstant.RESPONSE_PROCESS_CONTINUE;
    }

    /**
     * 
     * [描述]：查询第三方支付</br>
     *
     * @param billPayment
     * @param paymentQueryResult
     * @throws LandseaException
     *
     */
    @Override
    public void paymentQuery(PaymentQueryRequest paymentQuery, BillPayment billPayment, PaymentQueryResult paymentQueryResult, Map<String, Object> resultMap)
        throws LandseaException
    {
        // 智能POS没有查询接口
        paymentQueryResult.setNeedQueryProcess(false);
    }

    
    @Override
    public void buildResponseResult(BillPayment billPayment, PaymentHelperBean helperObject, Map<String, Object> resultMap)
        throws LandseaException
    {
        // 只要能走到这里，说明处理成功，不关心支付结果
        resultMap.put("status", OPERATE_SUCCESS);
        resultMap.put("msg", "success");
    }

    @Override
    protected String getProcessClassName()
    {
        return this.getClass().getSimpleName();
    }
}
